package com.hengpeng.itfints.controller.message;

import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.persist.Message;
import com.hengpeng.itfinbase.persist.Message.MessageType;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.itfints.controller.BaseController;
import com.hengpeng.itfints.tools.PageUtil;

@Controller
@RequestMapping("/message")
public class MessageController extends BaseController {
    private static final Integer MESSAGE_PAGE_SIZE = 10;

    @RequestMapping(value = "/messageList", method = RequestMethod.GET)
    @SuppressWarnings("unchecked")
    public String getMessageByUserName(HttpServletRequest request, String curPage, ModelMap map) {
        UserInfo userInfo = getSessionUserInfo(request);
        if (userInfo == null) {
            return returnErrorPage(map, "登录过期，请重新登录");
        }
        int currentPage = (!StringHelper.isEmpty(curPage) ? Integer.parseInt(curPage) : 1);

        LinkedHashMap<String, OrderType> sort = new LinkedHashMap<String, OrderType>();
        sort.put("m.creatDate", OrderType.DESC);

        ServiceResult<List<Message>> serviceResult = this.serviceFactory.getMessageService()
                .findMesWithPage(userInfo.getUserName(), MessageType.COMMON, currentPage, MESSAGE_PAGE_SIZE, sort);

        List<Message> list = serviceResult.getReturnValue();
        PageUtil pageUtil = new PageUtil(10, serviceResult.getReturnListSize(), currentPage);
        map.put("PageUtil", pageUtil);
        map.put("myMessageList", list);
        return "/page/account/user/message/messageList.jsp";
    }
}
